package com.bs.modules.use.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.bs.modules.use.entity.AppPriceEntity;
import com.bs.modules.use.service.AppPriceService;
import lombok.SneakyThrows;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bs.modules.use.entity.AppPowerEntity;
import com.bs.modules.use.service.AppPowerService;
import com.bs.common.utils.PageUtils;
import com.bs.common.utils.R;


/**
 * 电表
 *
 * @author lichenguang
 * @email 13031069255@163.com
 * @date 2022-04-05 20:38:57
 */
@RestController
@RequestMapping("use/apppower")
@Api(tags = "电表")
public class AppPowerController {

  @Autowired
  private AppPowerService appPowerService;

  @Autowired
  private AppPriceService appPriceService;

  @ApiOperation(value = "列表")
  @GetMapping("/list")
  @RequiresPermissions("use:apppower:list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = appPowerService.queryPage(params);

    return R.ok().put("page", page);
  }


  @ApiOperation(value = "详情")
  @GetMapping("/info/{id}")
  @RequiresPermissions("use:apppower:info")
  public R info(@PathVariable("id") Long id) {
    AppPowerEntity appPower = appPowerService.getById(id);

    return R.ok().put("appPower", appPower);
  }

  @SneakyThrows
  @ApiOperation(value = "保存")
  @PostMapping("/save")
  @RequiresPermissions("use:apppower:save")
  public R save(@RequestBody AppPowerEntity appPower) {

    AppPriceEntity appPriceEntity = appPriceService.findNewInfo();
    if (appPriceEntity == null) {
      return R.error("价格为空请联系管理员");
    }

    appPower.setPowerStatus("1");

    //获取这个月电表数
    int powerCoun = Integer.parseInt(appPower.getPowerCount());

    //获取上个月电表数
    int powerBeforecount = Integer.parseInt(appPower.getPowerBeforecount());

    // 这个月 -  上个月  = 用的电数
    int sum  = powerCoun - powerBeforecount;

    appPower.setPowerNumber(String.valueOf(sum));
    //根据电表价格id 价格查对应
    int  powerPriceid = Integer.parseInt(appPower.getPowerPriceid());
    Long b  = (long) powerPriceid;
    AppPriceEntity byId = appPriceService.getById(b);

    //电价格
    Double aDouble = Double.valueOf(byId.getPowerprice());
    Double  money =  sum * aDouble;
//    String format = String.format("%.2f", money);
//    System.out.println(format);

    appPower.setPowerMoneyone(String.format("%.2f", money));

    appPowerService.save(appPower);

    return R.ok();
  }


  @ApiOperation(value = "修改")
  @PostMapping("/update")
  @RequiresPermissions("use:apppower:update")
  public R update(@RequestBody AppPowerEntity appPower) {
    appPowerService.updateById(appPower);

    return R.ok();
  }

  @ApiOperation(value = "删除")
  @PostMapping("/delete")
  @RequiresPermissions("use:apppower:delete")
  public R delete(@RequestBody Long[] ids) {
    appPowerService.removeByIds(Arrays.asList(ids));

    return R.ok();
  }





}
